package cn.com.dingtek.genius.manager.server.web.controller;

import cn.com.dingtek.genius.manager.server.repository.db.entity.DeviceBase;
import cn.com.dingtek.genius.manager.server.service.StaffDeviceAppService;
import cn.com.dingtek.genius.manager.server.support.sam.annotation.AssertAuth;
import cn.com.dingtek.genius.manager.server.web.Result;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/staff_device_app")
public class StaffDeviceAppController {
    @Autowired
    private StaffDeviceAppService staffDeviceAppService;

    @AssertAuth
    @PostMapping("/device_list")
    public Result list(@RequestBody JSONObject json) {
        int currentPage = json.getInteger("current_page") == null ? 0 : json.getInteger("current_page");
        int pageSize = json.getInteger("page_size") == null ? 10 : json.getInteger("page_size");
        DeviceBase param = json.getObject("search_param", DeviceBase.class);
        Long staffId = json.getJSONObject("search_param").getLong("staffId");
        return Result.ok(staffDeviceAppService.list(param, staffId, currentPage, pageSize));
    }

    @AssertAuth
    @PostMapping("/bind")
    public Result bind(@RequestBody JSONObject json) {
        Long staffId = json.getLong("staffId");
        Long deviceId = json.getLong("deviceId");
        Integer appType = json.getInteger("appType");
        staffDeviceAppService.bind(staffId, deviceId, appType, DateTime.now().toDate());
        return Result.ok();
    }

    @AssertAuth
    @PostMapping("/unbind")
    public Result unbind(@RequestBody JSONObject json) {
        Long staffId = json.getLong("staffId");
        Long deviceId = json.getLong("deviceId");
        staffDeviceAppService.unbind(staffId, deviceId);
        return Result.ok();
    }
}
